/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Frames.Clientes;

import Clases.Cuit;
import Clases.MFecha;
import Entities.Clientes;
import Entities.ClientesContactos;
import Entities.Provincias;
import Entities.TiposIva;
import Frames.PANEL;
import java.awt.Image;
import java.awt.Toolkit;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Victor_2
 */
public class FRMClientes extends javax.swing.JFrame {

    MFecha Fecha = new MFecha();
    Cuit Cuit = new Cuit();
    private static org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger(FRMClientes.class);
    Controller.TiposIvaJpaController JPAIva = new Controller.TiposIvaJpaController(PANEL.EM);
    Controller.ClientesJpaController JPACliente = new Controller.ClientesJpaController(PANEL.EM);
    Controller.ProvinciasJpaController JPAProvincia = new Controller.ProvinciasJpaController(PANEL.EM); 
    Controller.ClientesContactosJpaController JPACC = new Controller.ClientesContactosJpaController(PANEL.EM);
    List<TiposIva> listaiva = new ArrayList<>();    
    List<Provincias> listaprovincia = new ArrayList<>(); 
    List<ClientesContactos> listacontactos = new ArrayList<>();
    DefaultTableModel modelo;
    ClientesContactos Contacto;
    private int filaeditar;    
    int idCliente = 0;
    
    /**
     * Creates new form FRMClientes2
     * @param idCliente
     */
    public FRMClientes(int idCliente) {
        this.idCliente = idCliente;
        Image icon = Toolkit.getDefaultToolkit().getImage(this.getClass().getResource("/Images/clientes.png"));
        this.setIconImage(icon);   
        initComponents();
    }
    
    public FRMClientes() {
        
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel1 = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        cmbiva = new javax.swing.JComboBox();
        jLabel13 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        txtcontacto = new javax.swing.JTextField();
        txtdomicilio = new javax.swing.JTextField();
        jLabel16 = new javax.swing.JLabel();
        cmbprovincia = new javax.swing.JComboBox();
        jLabel17 = new javax.swing.JLabel();
        txtweb = new javax.swing.JTextField();
        txtlocalidad = new javax.swing.JTextField();
        txttelefono = new javax.swing.JTextField();
        txtcorreo = new javax.swing.JTextField();
        jLabel14 = new javax.swing.JLabel();
        jLabel15 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        txtcliente = new javax.swing.JTextField();
        txtcuit = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        txtingb = new javax.swing.JTextField();
        jLabel12 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtobservaciones = new javax.swing.JTextArea();
        jPanel2 = new javax.swing.JPanel();
        jPanel5 = new javax.swing.JPanel();
        jScrollPane3 = new javax.swing.JScrollPane();
        tabla = new javax.swing.JTable();
        jLabel23 = new javax.swing.JLabel();
        txtnombrec = new javax.swing.JTextField();
        jLabel24 = new javax.swing.JLabel();
        txtcargoc = new javax.swing.JTextField();
        jLabel25 = new javax.swing.JLabel();
        txttelefonoc = new javax.swing.JTextField();
        jLabel26 = new javax.swing.JLabel();
        txtemailc = new javax.swing.JTextField();
        btnagregar = new javax.swing.JButton();
        btnquitar = new javax.swing.JButton();
        btneditar = new javax.swing.JButton();
        btnquitartodos = new javax.swing.JButton();
        jPanel6 = new javax.swing.JPanel();
        btncancelar = new javax.swing.JButton();
        cmbguardar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Clientes");
        addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentShown(java.awt.event.ComponentEvent evt) {
                formComponentShown(evt);
            }
        });

        jPanel3.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jLabel13.setText("Web");

        jLabel7.setText("IVA");

        jLabel16.setText("Contacto");

        jLabel17.setText("Localidad");

        jLabel14.setText("Provincia");

        jLabel15.setText("Domicilio");

        jLabel1.setText("Cliente *");

        jLabel4.setText("Cuit/Cuil");

        jLabel5.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel5.setText("Ing. Brutos");

        jLabel10.setText("Telefono");

        jLabel12.setText("E-mail");

        jLabel2.setText("Observaciones");

        txtobservaciones.setColumns(20);
        txtobservaciones.setRows(5);
        jScrollPane1.setViewportView(txtobservaciones);

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel4, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel14, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 54, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel3Layout.createSequentialGroup()
                                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(jPanel3Layout.createSequentialGroup()
                                        .addGap(159, 159, 159)
                                        .addComponent(jLabel17, javax.swing.GroupLayout.PREFERRED_SIZE, 54, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addGroup(jPanel3Layout.createSequentialGroup()
                                        .addGap(141, 141, 141)
                                        .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(jPanel3Layout.createSequentialGroup()
                                        .addComponent(txtingb, javax.swing.GroupLayout.PREFERRED_SIZE, 103, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(cmbiva, 0, 90, Short.MAX_VALUE))
                                    .addComponent(txtlocalidad)))
                            .addGroup(jPanel3Layout.createSequentialGroup()
                                .addGap(23, 23, 23)
                                .addComponent(txtcliente))))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jLabel13, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel16, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(42, 42, 42)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtweb)
                            .addComponent(txtcontacto)))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jScrollPane1))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jLabel15, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 54, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel10, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 54, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel12, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 54, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(38, 38, 38)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txttelefono, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(txtcorreo)
                            .addComponent(txtdomicilio)
                            .addGroup(jPanel3Layout.createSequentialGroup()
                                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(cmbprovincia, javax.swing.GroupLayout.PREFERRED_SIZE, 132, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(txtcuit, javax.swing.GroupLayout.PREFERRED_SIZE, 114, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(0, 0, Short.MAX_VALUE)))))
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtcliente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtcuit, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4)
                    .addComponent(jLabel5)
                    .addComponent(txtingb, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7)
                    .addComponent(cmbiva, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cmbprovincia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel14)
                    .addComponent(jLabel17)
                    .addComponent(txtlocalidad, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtdomicilio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel15))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txttelefono, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel10))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtcorreo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel12))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtcontacto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel16))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtweb, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel13))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 113, Short.MAX_VALUE))
                .addContainerGap())
        );

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        jTabbedPane1.addTab("Datos", jPanel1);

        jPanel5.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        tabla.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Nombre", "Cargo", "Telefonos", "Email"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tabla.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tablaMouseClicked(evt);
            }
        });
        jScrollPane3.setViewportView(tabla);

        jLabel23.setText("Nombre");

        jLabel24.setText("Cargo");

        jLabel25.setText("Telefonos");

        jLabel26.setText("Email ");

        btnagregar.setText("Agregar");
        btnagregar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnagregarActionPerformed(evt);
            }
        });

        btnquitar.setText("Quitar");
        btnquitar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnquitarActionPerformed(evt);
            }
        });

        btneditar.setText("Editar");
        btneditar.setEnabled(false);
        btneditar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btneditarActionPerformed(evt);
            }
        });

        btnquitartodos.setText("Quitar Todos");
        btnquitartodos.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnquitartodosActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel5Layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(btnagregar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnquitar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnquitartodos)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btneditar))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel5Layout.createSequentialGroup()
                        .addComponent(jLabel23, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(txtnombrec, javax.swing.GroupLayout.PREFERRED_SIZE, 185, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel24)
                        .addGap(27, 27, 27)
                        .addComponent(txtcargoc))
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 519, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel5Layout.createSequentialGroup()
                        .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel25, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel26))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txttelefonoc)
                            .addComponent(txtemailc))))
                .addGap(20, 20, 20))
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel23)
                        .addComponent(txtnombrec, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel24))
                    .addComponent(txtcargoc))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel25)
                    .addComponent(txttelefonoc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel26)
                    .addComponent(txtemailc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnquitartodos)
                    .addComponent(btneditar)
                    .addComponent(btnquitar)
                    .addComponent(btnagregar))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 207, Short.MAX_VALUE)
                .addContainerGap())
        );

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        jTabbedPane1.addTab("Datos de Contacto", jPanel2);

        jPanel6.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        btncancelar.setBackground(new java.awt.Color(255, 103, 56));
        btncancelar.setMnemonic('C');
        btncancelar.setText("Cancelar");
        btncancelar.setToolTipText("");
        btncancelar.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        btncancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btncancelarActionPerformed(evt);
            }
        });

        cmbguardar.setBackground(new java.awt.Color(128, 188, 149));
        cmbguardar.setMnemonic('G');
        cmbguardar.setText("Guardar");
        cmbguardar.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        cmbguardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmbguardarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel6Layout = new javax.swing.GroupLayout(jPanel6);
        jPanel6.setLayout(jPanel6Layout);
        jPanel6Layout.setHorizontalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel6Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(cmbguardar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btncancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jPanel6Layout.setVerticalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel6Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btncancelar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(cmbguardar))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 578, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jTabbedPane1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents

    private void formComponentShown(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_formComponentShown
        modelo = (DefaultTableModel) tabla.getModel();
        if (idCliente == 0){
            LlenarProvincias(); 
            limpiarcampos();
        }
        else{
            try {
                llenarcampos();
            } catch (Exception ex) {
                log.error(ex.getMessage());
            }
        }
    }//GEN-LAST:event_formComponentShown

    private void btncancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btncancelarActionPerformed
        this.dispose();
    }//GEN-LAST:event_btncancelarActionPerformed

    private void cmbguardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmbguardarActionPerformed

        String nombrerazon = txtcliente.getText();
        String cuit = txtcuit.getText();
        String ingb = txtingb.getText();
        String domicilio = txtdomicilio.getText();
        String localidad = txtlocalidad.getText();
        String tel = txttelefono.getText();
        String correo = txtcorreo.getText();
        String web  = txtweb.getText();
        String contacto = txtcontacto.getText();
        int indexiva = cmbiva.getSelectedIndex();
        int indexprov = cmbprovincia.getSelectedIndex();
        boolean errorcuit=false;
        if(!cuit.isEmpty()){
            errorcuit = Cuit.validar(cuit);
        }
        if(nombrerazon.isEmpty()){
            JOptionPane.showMessageDialog(rootPane, "Complete los Campos ");
            txtcliente.grabFocus();
        }
        else{
            if (errorcuit){
                JOptionPane.showMessageDialog(rootPane, "CUIT Erroneo");
                txtcuit.grabFocus();
            }
            else{
                Clientes Entity = new Clientes();
                Entity.setNombrerazon(new Clases.Caracteres().primeraMayuscula(nombrerazon));
                Entity.setContacto(contacto);
                Entity.setCuit(cuit);
                Entity.setDireccion(domicilio);
                Entity.setEmail(correo);
                Entity.setIdProvincias(listaprovincia.get(indexprov));
                Entity.setIdTiposiva(listaiva.get(indexiva));
                Entity.setIngbrutos(ingb);
                Entity.setLocalidad(localidad);
                Entity.setTelefono(tel);
                Entity.setWeb(web);
                Entity.setEstado(true);
                Entity.setObservaciones(txtobservaciones.getText());
                if(idCliente == 0){
                    try {
                        Entity.setSaldo(0);
                        JPACliente.create(Entity);
                        for(ClientesContactos item :listacontactos){
                            item.setIdClientes(Entity);
                            JPACC.create(item);
                        }
                        JOptionPane.showMessageDialog(rootPane, "Alta Exitosa");
                        this.limpiarcampos();
                    } catch (Exception ex) {
                        log.error("Error en Alta de Cliente "+ex.getMessage());
                        JOptionPane.showMessageDialog(rootPane, "Error al Crear el Cliente "+ex.getMessage());
                    }
                }
                else{
                    Entity.setId(idCliente);
                    try {
                        JPACliente.edit(Entity);
                        JPACC.BorrarPoridCliente(Entity);
                        for(ClientesContactos item :listacontactos){
                            item.setIdClientes(Entity);
                            JPACC.create(item);
                        }
                        JOptionPane.showMessageDialog(rootPane, "Actualización Exitosa");
                        this.dispose();
                    } catch (Exception ex) {
                        log.error("Error al actualizar de Clientes "+ ex.getMessage());
                        JOptionPane.showMessageDialog(rootPane, "Error en la Actualización "+ex.getMessage());
                    }
                }
            }
        }
    }//GEN-LAST:event_cmbguardarActionPerformed

    private void tablaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tablaMouseClicked
        SeleccionarContacto();
    }//GEN-LAST:event_tablaMouseClicked

    private void btnagregarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnagregarActionPerformed
        if(txtnombrec.getText().isEmpty()){
            JOptionPane.showMessageDialog(rootPane, "Ingrese un Nombre ");
            txtnombrec.grabFocus();
        }
        else{
            ClientesContactos Entidad = new ClientesContactos();
            Entidad.setNombre(new Clases.Caracteres().primeraMayuscula(txtnombrec.getText()));
            Entidad.setCargo(txtcargoc.getText());
            Entidad.setEmail(txtemailc.getText());
            Entidad.setTelefonos(txttelefonoc.getText());
            AgregarContacto(Entidad);
            txtnombrec.grabFocus();
        }
    }//GEN-LAST:event_btnagregarActionPerformed

    private void btnquitarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnquitarActionPerformed
        int sel = tabla.getSelectedRow();
        if (sel>=0){
            listacontactos.remove(sel);
            modelo.removeRow(sel);
            tabla.setModel(modelo);
            btnagregar.setEnabled(true);
            btneditar.setEnabled(false);
        }
    }//GEN-LAST:event_btnquitarActionPerformed

    private void btneditarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btneditarActionPerformed
        EditarContacto();
    }//GEN-LAST:event_btneditarActionPerformed

    private void btnquitartodosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnquitartodosActionPerformed
        for (int i=0;i<listacontactos.size();i++){
            modelo.removeRow(0);
        }
        listacontactos.clear();
        tabla.setModel(modelo);
    }//GEN-LAST:event_btnquitartodosActionPerformed

    private void EditarContacto(){       
        if(txtnombrec.getText().isEmpty()){
            JOptionPane.showMessageDialog(rootPane, "Ingrese un Nombre ");
            txtnombrec.grabFocus();
        }
        else{            
            Contacto.setNombre(new Clases.Caracteres().primeraMayuscula(txtnombrec.getText()));
            Contacto.setCargo(txtcargoc.getText());
            Contacto.setEmail(txtemailc.getText());
            Contacto.setTelefonos(txttelefonoc.getText());            

            listacontactos.get(filaeditar).setNombre(Contacto.getNombre());
            listacontactos.get(filaeditar).setCargo(Contacto.getCargo());
            listacontactos.get(filaeditar).setEmail(Contacto.getEmail());
            listacontactos.get(filaeditar).setTelefonos(Contacto.getTelefonos());

            tabla.setValueAt(Contacto.getNombre(), filaeditar, 0);
            tabla.setValueAt(Contacto.getCargo(), filaeditar, 1);
            tabla.setValueAt(Contacto.getTelefonos(), filaeditar, 2);
            tabla.setValueAt(Contacto.getEmail(), filaeditar, 3);

            txtnombrec.setText("");
            txtcargoc.setText("");
            txttelefonoc.setText("");
            txtemailc.setText("");
            txtnombrec.grabFocus();
            btnagregar.setEnabled(true);
            btneditar.setEnabled(false);

        }
    }

    private void SeleccionarContacto(){
        int sel = tabla.getSelectedRow();
        if (sel>=0){
            filaeditar = sel;
            Contacto = listacontactos.get(sel);
            txtnombrec.setText(Contacto.getNombre());
            txtcargoc.setText(Contacto.getCargo());
            txttelefonoc.setText(Contacto.getTelefonos());
            txtemailc.setText(Contacto.getEmail());
            txtnombrec.grabFocus();
            btnagregar.setEnabled(false);
            btneditar.setEnabled(true);
        }
    }

    private void AgregarContacto(ClientesContactos Entidad){
        Vector<Object> vect = new Vector<>();
        vect.addElement(Entidad.getNombre());
        vect.addElement(Entidad.getCargo());
        vect.addElement(Entidad.getTelefonos());
        vect.addElement(Entidad.getEmail());
        modelo.addRow(vect);
        tabla.setModel(modelo);

        listacontactos.add(Entidad);
        txtnombrec.setText("");
        txtcargoc.setText("");
        txttelefonoc.setText("");
        txtemailc.setText("");
        txtnombrec.grabFocus();
    }

    private void limpiarcampos(){
        txtcliente.setText("");       
        txtcuit.setText("");
        txtingb.setText("");
        txtdomicilio.setText("");
        txtlocalidad.setText("");
        txttelefono.setText("");
        txtcorreo.setText("");
        txtweb.setText("");
        txtcontacto.setText("");        
        txtobservaciones.setText("");        
        llenariva();
        LlenarProvincias();         
        txtnombrec.setText("");
        txtcargoc.setText("");
        txtemailc.setText("");
        txttelefono.setText("");
        btnquitartodos.doClick();
    }    

    private void llenarcampos(){        
        try {
            List<Clientes> lista = JPACliente.ListarPorId(idCliente);
            txtcliente.setText(lista.get(0).getNombrerazon());             
            txtcuit.setText(lista.get(0).getCuit());
            txtingb.setText(lista.get(0).getIngbrutos());
            txtdomicilio.setText(lista.get(0).getDireccion());
            txttelefono.setText(lista.get(0).getTelefono());
            txtcorreo.setText(lista.get(0).getEmail());
            txtweb.setText(lista.get(0).getWeb());
            txtcontacto.setText(lista.get(0).getContacto());
            txtobservaciones.setText(lista.get(0).getObservaciones());
            txtlocalidad.setText(lista.get(0).getLocalidad());           
            llenariva(); 
            cmbiva.setSelectedItem(lista.get(0).getIdTiposiva().getTipos());            
            LlenarProvincias();                       
            cmbprovincia.setSelectedItem(lista.get(0).getIdProvincias().getDescripcion());            
            listacontactos.clear();
            LlenarTablaContactos(JPACC.ListarPoridCliente(idCliente));
        } catch (Exception ex) {            
            log.error("llenarcampos "+ex.getMessage());
        }
    }   
    
    
    private void LlenarTablaContactos(List<ClientesContactos> contactos){
        for(ClientesContactos Entidad :contactos){
            Vector<Object> vect = new Vector<>();
            vect.addElement(Entidad.getNombre());
            vect.addElement(Entidad.getCargo());
            vect.addElement(Entidad.getTelefonos());
            vect.addElement(Entidad.getEmail());
            modelo.addRow(vect);
            tabla.setModel(modelo);
            listacontactos.add(Entidad);
            txtnombrec.setText("");
            txtcargoc.setText("");
            txttelefonoc.setText("");
            txtemailc.setText("");
            txtnombrec.grabFocus();
        }
    }
    
    private void llenariva(){
        try {
            cmbiva.removeAllItems();
            listaiva= JPAIva.Listar();
            int i =0;
            for (i=0;i<listaiva.size();i++){
                cmbiva.addItem(listaiva.get(i).getTipos());
            }
        } catch (Exception ex) {
            log.error(ex.getMessage());
        }        
    }
    
    private void LlenarProvincias(){        
        try {
            cmbprovincia.removeAllItems();
            listaprovincia = JPAProvincia.Listar();
            int i =0;
            for (i=0;i<listaprovincia.size();i++){                               
                cmbprovincia.addItem(listaprovincia.get(i).getDescripcion());
            }            
        } catch (Exception ex) {
            log.error(ex.getMessage());
        }
    }
    
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(FRMClientes.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(FRMClientes.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(FRMClientes.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(FRMClientes.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new FRMClientes().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnagregar;
    private javax.swing.JButton btncancelar;
    private javax.swing.JButton btneditar;
    private javax.swing.JButton btnquitar;
    private javax.swing.JButton btnquitartodos;
    private javax.swing.JButton cmbguardar;
    private javax.swing.JComboBox cmbiva;
    private javax.swing.JComboBox cmbprovincia;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel17;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel23;
    private javax.swing.JLabel jLabel24;
    private javax.swing.JLabel jLabel25;
    private javax.swing.JLabel jLabel26;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JTable tabla;
    private javax.swing.JTextField txtcargoc;
    private javax.swing.JTextField txtcliente;
    private javax.swing.JTextField txtcontacto;
    private javax.swing.JTextField txtcorreo;
    private javax.swing.JTextField txtcuit;
    private javax.swing.JTextField txtdomicilio;
    private javax.swing.JTextField txtemailc;
    private javax.swing.JTextField txtingb;
    private javax.swing.JTextField txtlocalidad;
    private javax.swing.JTextField txtnombrec;
    private javax.swing.JTextArea txtobservaciones;
    private javax.swing.JTextField txttelefono;
    private javax.swing.JTextField txttelefonoc;
    private javax.swing.JTextField txtweb;
    // End of variables declaration//GEN-END:variables
}
